
local base64 = require 'core.util.base64'
local player = require 'core.types.player'

--收到魔兽端消息处理
local allowEvents = {}
ac.allowEvents = allowEvents    --允许交互事件

RegisterEvent('war3ToCloud',function(id, ename, str, player_index)
    local player = ac.player[player_index]
    local data = json.decode(str)
    if data then
        local name = data.name
        if name and allowEvents[name] then
            local ename = ('玩家-%s'):format(name)
            local params = data.params or {}
            player:notify(ename,player,table.unpack(params))
        end
    end
end)


--给魔兽端发送数据 /同帧不要同步太多数据，尽量岔开
local sync_list = {}
local page_byte = 500
local page_index = 0
function player.__index:sendWar3(name,...)
    local json_encode = json.encode({
        name = name,
        params = {...},
    })
    local page = math.ceil(#json_encode/page_byte)
    if page>1 then
        page_index = page_index + 1
        for a=1,page do
            local str = json_encode:sub((a-1)*500 + 1,a*500)
            local data = json.encode({
                time = page_index,
                name = name,
                info = {str},
                page = a,
                page_max = page,
            })
            MsSendMlEvent(self.id, 'cloudToWar3_page', data)
        end
    else
        MsSendMlEvent(self.id, 'cloudToWar3', json_encode)
    end
end

